//
//  CardTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit

class CardTableViewCell: UITableViewCell {
    var collectionView: UICollectionView!
    var carModelArray: [CarModelDataList] = []
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        let layout = UICollectionViewFlowLayout()
//        layout.minimumLineSpacing = 10
//        layout.itemSize = CGSize(width: 70, height: 70)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: 170), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.register(CardCollectionViewCell.self, forCellWithReuseIdentifier: "CardCollectionViewCell")
        self.contentView.addSubview(self.collectionView)
        
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.greaterThanOrEqualTo(160)
            make.bottom.equalTo(-10)
        }
        
    }

    
    //setModel vehicleModelArray
    func setCardTableViewCell(data: [CarModelDataList]){
        if data.count > 0 {
            self.carModelArray = data
            self.collectionView.reloadData()
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(5)
                make.height.greaterThanOrEqualTo(160)
                make.bottom.equalTo(-10)
            }
        }else{
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(0)
                make.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(0)
            }
        }
        self.updateConstraints()
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


extension CardTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: 70, height: 70)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return ((kScreenWidth - 28) - 4 * 70)/3 - 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.carModelArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCollectionViewCell", for: indexPath) as! CardCollectionViewCell
        if self.carModelArray.count > indexPath.row{
            let model = self.carModelArray[indexPath.row]
            cell.setCardCollectionViewCell(model: model)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row != 7 && self.carModelArray.count > indexPath.row {
            let model = self.carModelArray[indexPath.row]
            let tempModel = SelectparameterModel.init()
            tempModel.carCategoryIds = "\(model.id ?? 0)" //"\(indexPath.row + 1)"
            let vc = UsedCarViewController()
            vc.selectParam = tempModel
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }else{
            let vc = ScreenViewController()
            vc.isHomeEnter = 1
//            let navi = BaseNavigationController(rootViewController: vc)
//            navi.modalPresentationStyle = .fullScreen
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)

//            self.currentViewController()?.present(navi, animated: true, completion: nil)
            
        }
    }
}
